﻿using HCMS.CommonDtos;
using HCMS.Const;
using HCMS.DomainServiceManage.Cache.Manage;
using HCMS.Entities.Manage;
using HCMS.Extensions;
using HCMS.IRepositories.Manage;
using HCMS.IServices.Manage;
using HCMS.Repositories.Manage;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using System.Linq;

namespace HCMS.Services.Manage
{
    /// <summary>
    /// 管理员 应用服务
    /// </summary>
    public class AdminAppService : CommonAppService<ManageDbContext, Admin>, IAdminAppService
    {
        private readonly IRepository<Admin> baseAdminRepository;
        private readonly IAdminRepository adminRepository;
        private readonly IAdminRoleRepository adminRoleRepository;
        private readonly IRoleRepository roleRepository;
        private readonly AdminCacheManager adminCacheManager;

        public AdminAppService(
            IRepository<Admin> baseAdminRepository,
            IAdminRepository adminRepository,
            IAdminRoleRepository adminRoleRepository,
            IRoleRepository roleRepository,
            AdminCacheManager adminCacheManager
            ) : base(baseAdminRepository)
        {
            this.baseAdminRepository = baseAdminRepository;
            this.adminRepository = adminRepository;
            this.adminRoleRepository = adminRoleRepository;
            this.roleRepository = roleRepository;
            this.adminCacheManager = adminCacheManager;
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<object>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            var admin = await adminRepository.ManageList(searchArgs, pager);

            var result = new ListPagerDto<object>();
            result.PagerInfo = admin.PagerInfo;

            var data = new List<object>();
            foreach (var item in admin.Data)
            {
                // 关联 角色信息
                var roleName = string.Empty;

                if (ManageConst.SuperAdminName.Contains(item.AdminName))
                {
                    roleName = "超级管理员";
                }
                else
                {
                    var adminRole = await adminRoleRepository.FirstOrDefaultAsync(t => t.AdminId == item.Id && t.Status == 0);
                    if (adminRole != null)
                    {
                        var role = await roleRepository.FirstOrDefaultAsync(t => t.Id == adminRole.RoleId && t.Status == 0);
                        if (role != null)
                        {
                            roleName = role.Name;
                        }
                    }
                }

                var onlineStatus = await adminCacheManager.GetOnlineStatus(item.Id);

                data.Add(new
                {
                    id = item.Id,
                    adminName = item.AdminName,
                    logo = item.Logo.DomainImages(),
                    status = item.Status,
                    updateTime = item.UpdateTime,
                    updateUser = item.UpdateUser,
                    createTime = item.CreateTime,
                    createUser = item.CreateUser,
                    roleName = roleName,
                    onlineStatus = onlineStatus,
                    loginCurrentTime = item.LoginCurrentTime
                });
            }

            result.Data = data;

            return result;
        }

        /// <summary>
        /// 设置登录信息
        /// </summary>
        /// <remarks>
        /// 人员进行登录、更新管理员密码、状态，都需要执行此方法
        /// </remarks>
        /// <param name="adminId">管理员 Id</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="ts">保存时间</param>
        /// <returns></returns>
        public async Task<bool> SetLoginedInfo(int adminId, long timestamp, TimeSpan ts)
        {
            var result = await adminCacheManager.SetLoginedInfo(adminId, timestamp, ts);
            return result;
        }

        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <remarks>
        /// 无值、无效，则管理员下线
        /// </remarks>
        /// <param name="adminId">管理员 Id</param>
        /// <returns></returns>
        public async Task<string> GetLoginedInfo(int adminId)
        {
            var result = await adminCacheManager.GetLoginedInfo(adminId);
            return result;
        }

        /// <summary>
        /// 获取全部管理员
        /// </summary>
        /// <returns></returns>
        public async Task<List<Admin>> AllList()
        {
            return await adminRepository.AllList();
        }
    }
}
